/**
 * 进入页表判断
 */
var  timeTaskH;
$(document).ready(function(){
	if($("#pagination").length!=0){
		$(".page_content").hide();
		timeTaskH =setInterval(reloadAllbyAll,5);
  }
});

window.dg_cm_pp={
		
	}

function downSelCon1(){
    $("tr[name='selCon']").show();
    $("#downSelCon").hide();
    $("#upSelCon").show();
}

function upSelCon1(){
	$("tr[name='selCon']").hide();
    $("#downSelCon").show();
    $("#upSelCon").hide();
}


/**
 * 模糊查询方法
 */
function search_H() {
	 var $tr=$(".boxBmargin12.cell table tbody tr");
	 var $a=$($tr[$tr.length-1]).children().children('a:contains("查")');
     $a.click();
}
//改写弹框页
createSimpleWindow=function(pageName,title,url,width,heigth){
	   layer.open({
	                type: 2,
	                title: title,
	                shadeClose: true,
	                shade: 0.8,
	                area: ['65%', '80%'],
	                content: url,
	                cancel:function (index) {
	                    layer.close(index);
	                    $("#searchH").click();
	                },
	                end:function (index) {
	                    layer.close(index);
	                    $("#searchH").click();
	                }
	            });
}
//改写弹框
$.messager.alert=function(title,content){
	layer.alert(content, {icon: 6});
}

/**
 * 添加用户信息
 */
function addCode(title,content){
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.8,
        area: ['65%', '80%'],
        content: content,
        cancel:function (index) {
            layer.close(index);
            search();
        },
        end:function (index) {
            layer.close(index);
            search();
        }
    });

}
function changeBodyH(){
    $("body").append(" <div class='box-body' style='display: block;'><div id='search' style='padding:3px' class='pull-left'><table id='searchTable' style='width: 100%;marging-left:50px'></table></div><div class='box-tools pull-right'></div></div>");
        //获取所有的tbody对象
        var $tr=$(".boxBmargin12.cell table tbody tr");
        //查询条件定位器
        var searchCount=1;
        //插入定位器
        var indexCount=1;
        
        var idTr=1;
        $("#searchTable").append("<tr height='50px' style='line-height:15px'><td style='width: 24%;' id='1_1'></td><td style='width: 24%;' id='1_2'></td><td style='width: 24%;' id='1_3'></td><td style='width: 24%;' id='1_4'></td><td style='width: 24%;' id='1_5'></td></tr>");
        for(var i=0;i<$tr.length-1;i++){
        	var th=$($tr[i]).children('th');
        	var td=$($tr[i]).children('td');
        	for(var j=0;j<th.length;j++){
        		if(indexCount==5){
        			idTr++;
            		$("#searchTable").append("<tr name='selCon' style='display:none'><td id='"+idTr+"_1'></td><td id='"+idTr+"_2'></td><tdid='"+idTr+"_3'></td><td id='"+idTr+"_4'></td><td id='"+idTr+"_5'></td></tr>");
            		indexCount=1;
            	}
        		console.log($("#"+idTr+" td[name="+indexCount+"]"));
        		$("#"+idTr+"_"+indexCount).append("<label>"+$(th[j]).text()+"</label>"+$(td[j]).html());
        		searchCount++;
        		indexCount++;
        	}
        	//拼接好之后删除元素 防止重复
        	$($tr[i]).remove();
        }
        $("#searchTable tr td").children(1).css("padding","6px 12px").css("width","130px");
    	
        if(searchCount<6){
    		$("#1_"+indexCount)
    		.css("width","4%").css("text-align","left")
    		.append("<button type='button' class='btn btn-primary' id='searchH' onclick='search_H()'>查询</button>");
    	}else{
    		$("#1_5")
    		.css("width","4%").css("text-align","left")
    		.append("<button type='button' class='btn btn-primary' id='searchH' onclick='search_H()'>查询</button>");
    		$("#searchTable").append("<tr><td></td><td></td><td><span class='input-group-btn'><button id='downSelCon' onclick='downSelCon1()' type='button' class='btn btn-default' style='border:0;display:show'><i class='glyphicon glyphicon-chevron-down'></i></button><button id='upSelCon' onclick='upSelCon1()' type='button' class='btn btn-default' style='border:0;display:none'><i class='glyphicon glyphicon-chevron-up'></i></button></span></td></tr>");
    	}
    	
    	
    }

function reloadAllbyAll(){
	if($('#pagination').datagrid('options').url!=''){
		clearInterval(timeTaskH);
		changeBodyH();
        loadGrid();
        $("#codeName").addClear();
        $("#codeName").bind("keypress",function(event){
            if(event.keyCode == 13){
                search();
            }
        });
	}
	
}
    var rowHeight = 32;
    //加载表格datagrid
    function loadGrid() {
        var url=$('#pagination').datagrid('options').url;
        var columns=$('#pagination').datagrid('options').columns;
        var frozenColumns=$('#pagination').datagrid('options').frozenColumns;
        var toolbar=$('#pagination').datagrid('options').toolbar;
        if(frozenColumns.length!=0){
        	columns[0].unshift(frozenColumns[0][0]);
        }
        var widthTotal=0;
        for(var i=0;i<columns[0].length;i++){
        	if(!isNaN(columns[0][i].width)){
        		widthTotal=widthTotal+columns[0][i].width;
        	}
        }
        for(var i=0;i<columns[0].length;i++){
        	if(!isNaN(columns[0][i].width)){
        		var widthValTemp=columns[0][i].width/widthTotal*95+"%";
        		columns[0][i].width=widthValTemp;
        	}
            columns[0][i].align="center";
            columns[0][i].sortable="true";
        }
        //清除原有的#pagination
        $("#pagination").remove();
        $("body").append("<div class='easyui-datagrid' id='pagination'></div>");
        var clientH = document.documentElement.clientHeight;
        var offsetH =clientH - 61;
        var pageSize = 10;
        rowHeight = (offsetH-55) / pageSize;
        //加载数据
        $('#pagination').datagrid({
            width:'100%',
            height:offsetH,
            idField: 'id',
            nowrap: false,
            striped: true,
            border: true,
            loadMsg: "数据加载中，请稍后...",
            collapsible:false,//是否可折叠的
            fit: false,//自动大小
            pagination:true,//分页控件
            rownumbers:true,//行号
            fitcolumns:true,
            url: url,
            method: 'post',
            columns: columns,
            onLoadSuccess: function(data){
                var dataGrid = $("#pagination");
                autoHeight(dataGrid,rowHeight);
            }
        });
        //设置分页控件
        var p = $('#pagination').datagrid('getPager');
        $(p).pagination({
            pageSize: 2147483647,////每页显示的记录条数，默认为10
            beforePageText: '第',//页数文本框前显示的汉字
            afterPageText: '页    共 {pages} 页',
            displayMsg: '共 {total} 条记录',
        });
        //绑定添加按钮
        if(toolbar.length!=0){
        	for(var i =0;i<toolbar.length;i++){
        		if(toolbar[i].iconCls=='icon-add'){
        			$(".box-tools.pull-right").append("<input id='addNewH' class='btn btn-success' type='button' value='添加'/>");
        			$("#addNewH").bind("click",toolbar[i].handler);
        			 //获取元素中绑定方法并进行绑定
        		}else if(toolbar[i].iconCls=='icon-remove'){
        			$(".box-tools.pull-right").append("<input id='deleteNewH' class='btn btn-danger' type='button' value='删除'/>");
        			$("#deleteNewH").bind("click",toolbar[i].handler);
        		}
        	}
        }
       
    }